import { ref, computed } from 'vue';

export function useTreeSearch(treeData) {
  const searchKeyword = ref('');

  const filteredTreeData = computed(() => {
    if (!searchKeyword.value) {
      return treeData;
    }

    const filterNode = (node) => {
      if (node.label.toLowerCase().includes(searchKeyword.value.toLowerCase())) {
        return true;
      }

      if (node.children) {
        return node.children.some(filterNode);
      }

      return false;
    };

    return treeData.filter(filterNode);
  });

  return {
    searchKeyword,
    filteredTreeData,
  };
}
